<template>
  <div class="rechargeCard">
    <x-header :left-options="{backText: ''}">充值</x-header>
    <div style="color:#999;font-size:15px;margin-left:15px;height: 30px;line-height: 30px">
      选择支付方式
    </div>
    <div style="height:60px;line-height: 60px;background:#fff;padding-left:15px;position: relative;" @click="choiceBank()" v-if="bankList.length==0">
      <span>请选择银行卡</span>
      <img src="../../assets/nav.png" alt="" width="8" style="position: absolute;right: 15px;top:20px;">
    </div>
    <div style="height:60px;line-height: 60px;background:#fff;padding-left:15px;position: relative;" v-for="bank in bankList"
      @click="choiceBank()" v-if="bankList.length>0">
      <img :src="bank.url[3]" alt="" height="25" style="vertical-align: middle;" v-if="bank.url">
      <span>{{bank.name}}</span>
      <img src="../../assets/nav.png" alt="" width="8" style="position: absolute;right: 15px;top:20px;">
    </div>
    <div class="mT10">
      <div class="bankDetail">
        <span class="c9">收款方户名</span>
        <span class="c9 fr fr15">浦发银行</span>
      </div>
      <div class="bankDetail">
        <span class="c9">收款方账户</span>
        <!-- 用于放置复制的内容 -->
        <input id="foo" class="c9 fr fr15" value="6217001210029664974" style="border:none;outline: none;text-align: right;right: 70px;top: 13px;background:#fff;"
          readonly>
        <!-- 用于触发复制功能的按钮 -->
        <div class="c9 fr fr15 copyBtn">
          <copy :show="true" :value="11" :text="'复制'" :id="'#foo'"></copy>
        </div>
      </div>
      <div class="bankDetail">
        <span class="c9">收款方银行</span>
        <span class="c9 fr fr15">{{bankName}}</span>
      </div>
      <div class="bankDetail">
        <span class="c9">开户银行</span>
        <span class="c9 fr fr15">{{bankDetailAddress}}</span>
      </div>
    </div>
    <div class="bankDetail mT10">
      <span class="c9">充值金额</span>
      <input placeholder="请输入充值金额" class="fr15 fr bankMoney" v-model="bankMoney" type="number">
    </div>
    <div class="confimBankBox" @click="confimBankBtn">
      <div class="confimBank">确认充值</div>
    </div>
    <!-- 输入支付密码 -->
    <div class="tips" v-show="showTips">
      <div class="tips-top">
        <div style="text-align: center;height: 45px;line-height: 45px;border-bottom: 1px solid #f6f6f6;">
          <img src="../../assets/close.png" alt="" width="15" @click="closeShowTips">
          <span class="c3">请输入支付密码</span>
        </div>
        <div>
          <div class="tips-center">
            <li>
              <input type="password" v-model="pwd1" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd2" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd3" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd4" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd5" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd6" maxlength="1" @keyup="checkPwd" class="input">
            </li>
          </div>
          <div class="forgetPassWord" @click="$router.push({path:'/main/forgetPay'})">忘记密码?</div>
        </div>
      </div>
    </div>
    <!-- 支付密码错误弹框 -->
    <div class="errTips" v-if="errTips">
      <div class="errTipsBox">
        <div class="errTips-top">
          支付密码错误,请重试!
        </div>
        <div class="errTipsBtn">
          <span class="active" @click="retry">重试</span>
          <span @click="$router.push({path:'/main/forgetPay'})">忘记密码?</span>
        </div>
        <div style="margin-left: 30px;font-size: 13px;margin-top: 5px;color: #ff1010">(说明：只能重试3次)</div>
      </div>
    </div>
    <!-- 设置支付密码 -->
    <div class="setPassWorldTips" v-if="isPassWorld">
      <div class="setPasswordText">
        <div class="setPassWorldTitle">请设置支付密码!</div>
        <div class="goPassworld" @click="$router.push({path:'/main/setPayPassword'})">立即前往</div>
      </div>
    </div>
  </div>
</template>
<script>
  import { XHeader } from 'vux'
  import Copy from '../../components/common/copy.vue'
  import tips from '../../service/tips.js'
  import http from '../../service/http.js'
  import util from '../../service/util.js'
  export default {
    data() {
      return {
        bankMoney: '',
        id: "",
        bankList: [],
        bankName: "",
        bankDetailAddress: "",
        showTips: false,//显示支付密码弹框
        errTips: false,//显示支付错误弹框
        isPassWorld: false,//判断有没有支付密码
        pwd: "",
        pwd1: "",
        pwd2: "",
        pwd3: "",
        pwd4: "",
        pwd5: "",
        pwd6: "",
      }
    },
    mounted() {
      // this.id = location.hash.split("=")[1];
      this.id = this.$route.query.id;
      this.getBankList();
      var txts = document.querySelectorAll(".input");
      for (var i = 0; i < txts.length; i++) {
        var t = txts[i];
        t.index = i;
        t.setAttribute("readonly", true);
        t.onkeyup = function () {
          this.value = this.value.replace(/^(.).*$/, '$1');
          var next = this.index + 1;
          if (next > txts.length - 1) return;
          txts[next].removeAttribute("readonly");
          txts[next].focus();
        }
      }
      txts[0].removeAttribute("readonly");
    },
    methods: {
      // 关闭支付密码输入弹框
      closeShowTips() {
        this.showTips = false;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      // 重试按钮
      retry() {
        this.errTips = false;
        this.showTips = true;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      //判断有无支付密码
      hasPay() {
        http.post('/user/hasPay', {
          username: window.localStorage.getItem("login.username")
        }).then((res) => {
          if (res.status == 200 && !res.data.body) {//无支付密码
            this.isPassWorld = true;
          }
          if (res.status == 200 && res.data.body) { //有支付密码(下单)
            this.showTips = true;
          }
        })
      },
      //校验支付密码
      checkPwd() {
        this.pwd = this.pwd1 + this.pwd2 + this.pwd3 + this.pwd4 + this.pwd5 + this.pwd6;
        if (this.pwd1 && this.pwd2 && this.pwd3 && this.pwd4 && this.pwd5 && this.pwd6) {
          http.post('/user/authPay', {
            payPwd: util.md5(this.pwd)
          }).then((res) => {
            if (res.data.status == 200) {
              http.put("/user/coin/charge", {
                payAmount: this.bankMoney,
                bankId: this.id,
                name: this.$route.query.type
              }).then((res) => {
                if (res.status == 200) {
                  // tips.tips(res.data.msg);
                  this.$router.push({ path: "/main/applicationRecharge" })
                } else {
                  tips.tips("充值失败");
                }
              }).catch((res) => {
                tips.tips(res.data.msg);
              })
            }
          })
        }
      },
      choiceBank() {
        this.$router.push({ path: '/main/choiceBank?status=1' })
      },
      //获取全部银行卡列表
      getBankList() {
        http.get('/user/bank').then((res) => {
          if (res.status == '200' && !this.id) {
            this.bankList = [];
            this.bankList.push(res.data.body[0]);
            this.bankList.forEach((v) => {
              this.bankName = v.name.split("银行")[0] + "银行"
              this.bankDetailAddress = v.name.split("银行")[1];
              this.id = v.id;
            })
          } else if (res.status == '200' && this.id) {
            res.data.body.forEach((v1) => {
              if (v1.id == this.id) {
                this.bankList = [];
                this.bankList.push(v1);
              }
            })
            this.bankList.forEach((v) => {
              this.bankName = v.name.split("银行")[0] + "银行";
              this.bankDetailAddress = v.name.split("银行")[1];
              this.id = v.id;
            })
          }
        })
      },
      confimBankBtn() {
        if(!this.id){
          tips.tips("请选择银行卡");
          return false;
        }
        if (!this.bankMoney) {
          tips.tips("请输入充值金额");
          return false;
        }
        //判断有无支付密码
        this.hasPay();
      }
    },
    components: {
      XHeader,
      Copy
    }
  }
</script>
<style lang="less">
  body {
    background: #f6f6f6;
  }

  .rechargeCard .c9 {
    color: #999;
  }

  .rechargeCard .fr {
    position: absolute;
  }

  .rechargeCard .fr15 {
    right: 15px;
  }

  .rechargeCard .mT10 {
    margin-top: 10px;
  }

  .rechargeCard .copyBtn {
    color: #fff;
    background: linear-gradient(left, #fedd32, #fe9f0a);
    display: inline-block;
    width: 50px;
    text-align: center;
    height: 20px;
    line-height: 20px;
    font-size: 13px;
    top: 12px;
  }

  .rechargeCard .bankMoney {
    border: none;
    outline: none;
    text-align: right;
    display: inline-block;
    height: 45px;
    width: 70%;
    font-size: 15px;
  }

  .rechargeCard .vux-header {
    background: #fff;
  }

  .rechargeCard .vux-header .vux-header-title {
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 400;
    color: #000;
  }

  .rechargeCard .vux-header .vux-header-left .left-arrow:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid #000;
    border-width: 1px 0 0 1px;
    -webkit-transform: rotate(315deg);
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }

  .rechargeCard .bankDetail {
    border: 1px solid #f6f6f6;
    height: 45px;
    line-height: 45px;
    background: #fff;
    padding-left: 15px;
    position: relative;
  }

  .rechargeCard .confimBankBox {
    position: relative;
    padding: 0px 15px;
    margin-top: 60px;
  }

  .rechargeCard .confimBankBox .confimBank {
    width: 100%;
    height: 45px;
    color: #fff;
    line-height: 45px;
    text-align: center;
    background: linear-gradient(left, #fedd32, #fe9f0a);
    background: -webkit-gradient(linear, left top, right top, from(#fedd32), to(#fe9f0a));
    border-radius: 25px;
  }

  .rechargeCard .tips {
    position: fixed;
    background: rgba(0, 0, 0, 0.3);
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    z-index: 999;
    .tips-top {
      position: absolute;
      top: 40%;
      height: 60%;
      background: #fff;
      width: 100%;
      border-bottom: 1px solid #f6f6f6;
      img {
        position: absolute;
        left: 15px;
        top: 15px;
      }
    }
  }

  .rechargeCard .tips-center {
    padding-left: 10%;
    margin-top: 45px;
  }

  .rechargeCard .tips-center li {
    float: left;
    width: 15%;
    list-style: none;
    height: 45px;
    line-height: 45px;
    border: 1px solid #ccc;
    border-right: none;
    overflow: hidden;
    text-align: center;
    input {
      border: none;
      outline: none;
      height: 45px;
      line-height: 45px;
      width: 100%;
      display: block;
      text-align: center;
    }
  }

  .rechargeCard .tips-center li:last-child {
    border-right: 1px solid #ccc;
  }

  .rechargeCard .forgetPassWord {
    position: absolute;
    right: 28px;
    top: 150px;
    color: #fea620;
  }

  .rechargeCard .errTips {
    position: fixed;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .rechargeCard .errTipsBox {
    position: absolute;
    top: 40%;
    width: 90%;
    height: 130px;
    background: #fff;
    margin-left: 5%;
    border-radius: 15px;
    .errTips-top {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
    }
    .errTipsBtn {
      span {
        display: inline-block;
        width: 48%;
        text-align: center;
        margin-top: 15px;
        color: #fea620;
      }
      .active {
        border-right: 1px solid #efefef;
      }
    }
  }

  .rechargeCard .setPassWorldTips {
    position: fixed;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .rechargeCard .setPasswordText {
    position: absolute;
    top: 40%;
    height: 130px;
    background: #fff;
    width: 90%;
    margin-left: 5%;
    border-radius: 15px;
    .setPassWorldTitle {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
      color: #000;
    }
    .goPassworld {
      height: 70px;
      line-height: 65px;
      color: #fea620;
      text-align: center;
    }
  }
</style>